package com.turing.post.account.util;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.Date;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import com.turing.post.util.PCPSUtil;

/**
 * 主帐户
 * 
 * @author kz
 * @date 2012-12-17
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "MasterAccount")
public class MasterAccount implements Serializable {

	public MasterAccount() {
		super();
	}

	/**
	 * 主帐号
	 */
	private BigInteger masterAccNo;
	/**
	 * 发卡商NO
	 */
	private String issueCompanyNo;
	/**
	 * 卡号
	 */
	private String cardNo;
	/**
	 * 帐户户主ID
	 */
	private BigInteger ownerId;
	/**
	 * 余额
	 */
	private BigInteger balance;
	/**
	 * 开户时间
	 */
	private Date openTime;
	/**
	 * 上次记帐时间
	 */
	private Date lastAccountTime;
	/**
	 * 开户状态
	 */
	private String openStatus;
	/**
	 * 销户状态
	 */
	private String closeStatus;
	/**
	 * 冻结
	 */
	private String freeze;
	/**
	 * 冻结金额
	 */
	private BigInteger freezeAmount;
	/**
	 * 待审核充值资金
	 */
	private BigInteger authDeposit;
	/**
	 * 赎回待付资金
	 */
	private BigInteger authRedemp;
	/**
	 * 止付
	 */
	private String stopPayment;
	/**
	 * 备注
	 */
	private String remark;

	/**
	 * 正常
	 */
	public final static String MASTERACCOUNT_OPENSTATUS_NORMAL = "0";

	/**
	 * 开户撤销
	 */
	public final static String MASTERACCOUNT_OPENSTATUS_CANCELED = "1";

	/**
	 * 未销户
	 */
	public final static String MASTERACCOUNT_CLOSESTATUS_NONE = "0";
	/**
	 * 已销户
	 */
	public final static String MASTERACCOUNT_CLOSESTATUS_CLOSED = "1";
	/**
	 * 销户撤销
	 */
	public final static String MASTERACCOUNT_CLOSESTATUS_CLOSE_CANCELED = "2";

	/**
	 * 未冻结
	 */
	public final static String MASTERACCOUNT_FREEZE_NONE = "0";
	/**
	 * 已冻结
	 */
	public final static String MASTERACCOUNT_FREEZE_ALREADY = "1";
	/**
	 * 解冻结
	 */
	public final static String MASTERACCOUNT_FREEZE_FREE = "2";

	/**
	 * 未止付
	 */
	public final static String MASTERACCOUNT_STOPPAYMENT_NONE = "0";
	/**
	 * 已止付
	 */
	public final static String MASTERACCOUNT_STOPPAYMENT_ALREADY = "1";
	/**
	 * 解止付
	 */
	public final static String MASTERACCOUNT_STOPPAYMENT_FREE = "2";

	@Id
	@Column(name = "MASTERACCNO", nullable = false)
	public BigInteger getMasterAccNo() {
		return masterAccNo;
	}

	public void setMasterAccNo(BigInteger masterAccNo) {
		this.masterAccNo = masterAccNo;
	}

	@Column(name = "ISSUECOMPANYNO", nullable = false)
	public String getIssueCompanyNo() {
		return issueCompanyNo;
	}

	public void setIssueCompanyNo(String issueCompanyNo) {
		this.issueCompanyNo = issueCompanyNo;
	}

	@Column(name = "CARDNO", nullable = true)
	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	@Column(name = "OWNERID", nullable = true)
	public BigInteger getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(BigInteger ownerId) {
		this.ownerId = ownerId;
	}

	@Column(name = "BALANCE", nullable = false)
	public BigInteger getBalance() {
		return balance;
	}

	public void setBalance(BigInteger balance) {
		this.balance = balance;
	}

	@Column(name = "OPENTIME", nullable = false)
	public Date getOpenTime() {
		return openTime;
	}

	public void setOpenTime(Date openTime) {
		this.openTime = openTime;
	}

	@Column(name = "LASTACCOUNTTIME", nullable = false)
	public Date getLastAccountTime() {
		return lastAccountTime;
	}

	public void setLastAccountTime(Date lastAccountTime) {
		this.lastAccountTime = lastAccountTime;
	}

	@Column(name = "OPENSTATUS", nullable = false)
	public String getOpenStatus() {
		return openStatus;
	}

	public void setOpenStatus(String openStatus) {
		this.openStatus = openStatus;
	}

	@Column(name = "CLOSESTATUS", nullable = false)
	public String getCloseStatus() {
		return closeStatus;
	}

	public void setCloseStatus(String closeStatus) {
		this.closeStatus = closeStatus;
	}

	@Column(name = "FREEZE", nullable = false)
	public String getFreeze() {
		return freeze;
	}

	public void setFreeze(String freeze) {
		this.freeze = freeze;
	}

	@Column(name = "FREEZEAMOUNT", nullable = false)
	public BigInteger getFreezeAmount() {
		return freezeAmount;
	}

	public void setFreezeAmount(BigInteger freezeAmount) {
		this.freezeAmount = freezeAmount;
	}

	@Column(name = "AUTHDEPOSIT", nullable = false)
	public BigInteger getAuthDeposit() {
		return authDeposit;
	}

	public void setAuthDeposit(BigInteger authDeposit) {
		this.authDeposit = authDeposit;
	}

	@Column(name = "AUTHREDEMP", nullable = false)
	public BigInteger getAuthRedemp() {
		return authRedemp;
	}

	public void setAuthRedemp(BigInteger authRedemp) {
		this.authRedemp = authRedemp;
	}
	
	@Column(name = "STOPPAYMENT", nullable = false)
	public String getStopPayment() {
		return stopPayment;
	}

	public void setStopPayment(String stopPayment) {
		this.stopPayment = stopPayment;
	}

	@Column(name = "REMARK", nullable = true)
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	/**
	 * 增加发生额
	 * 
	 * @param amt
	 */
	public void addAmount(BigInteger amt) {
		this.setBalance(this.getBalance().add(amt));
	}

	/**
	 * 判断帐户是否已经关闭
	 * 
	 * @return
	 */
	public boolean alreadyClosed() {
		// 开户状态
		if (!PCPSUtil.compareObject(this.getOpenStatus(),
				MASTERACCOUNT_OPENSTATUS_NORMAL))
			return true;

		// 销户状态
		if (PCPSUtil.compareObject(this.getCloseStatus(),
				MASTERACCOUNT_CLOSESTATUS_CLOSED))
			return true;

		return false;
	}

	/**
	 * 检查当前状态是否在列表中
	 * 
	 * @param allowOpenStatus
	 * @param status
	 * @return
	 */
	private boolean checkOneStatus(Map<String, String> allowStatus,
			String status) {
		if (allowStatus == null || status == null)
			return true;
		return allowStatus.containsKey(status);
	}

	/**
	 * 检查记帐状态
	 * 
	 * @param allowOpenStatus
	 * @param allowCloseStatus
	 * @param allowStopPayment
	 * @return
	 */
	public boolean checkAccountStatus(Map<String, String> allowOpenStatus,
			Map<String, String> allowCloseStatus,
			Map<String, String> allowStopPayment) {
		// 检查开户状态
		if (!checkOneStatus(allowOpenStatus, this.getOpenStatus()))
			return false;
		// 检查销户状态
		if (!checkOneStatus(allowCloseStatus, this.getCloseStatus()))
			return false;
		// 检查止付状态
		// if (!checkOneStatus(allowStopPayment, this.getStopPayment()))
		// return false;

		return true;
	}
}
